package cn.wolfcode.crm.shiro;

import cn.wolfcode.crm.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

//修改默认身份认证过滤器中对认证结果的处理
//给我们自定义的登陆过滤器贴上注解才能找到
@Component("crmFormAuthenticationFilter")
public class CRMFormAuthenticationFilter extends FormAuthenticationFilter{

    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        //响应页面中需要的数据
        JsonResult result = new JsonResult();
        response.getWriter().print(JSON.toJSONString(result));
        return false;
    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
        response.setContentType("application/json;charset=utf-8");
        //响应页面中需要的数据
        JsonResult result = new JsonResult();
        try {
            result.mark("账号或者密码错误");
            response.getWriter().print(JSON.toJSONString(result));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return false;
    }

}
